//#define TESTING
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Collections.Specialized;
using Microsoft.Office.Project.EPMAuditing.Library;
using Microsoft.Office.Project.EPMAuditing.ReceiverService.Monitor.Properties;

namespace Microsoft.Office.Project.EPMAuditing.ReceiverService.Monitor
{
    public partial class FormDisplay : Form
    {

        private List<ServiceControl> _controlList = new List<ServiceControl>();

        public FormDisplay()
        {
            InitializeComponent();
        }

        private int _widthOffset;
        private int _heightOffset;

        private void frmDisplay_Load(object sender, EventArgs e)
        {
            ServiceControl serviceControl;
            int x = 0;
            int y = 0;
            int formWidth = 0;
            int formHeight = 0;

            Rectangle clientArea= this.ClientRectangle;
            _widthOffset = this.Width - clientArea.Width;
            _heightOffset = this.Height - clientArea.Height;

#if TESTING
            StringCollection services = new StringCollection();
            String[] serviceArr = new String[] { "FwcAgent", "InoTask", "MSSQL$SQLEXPRESS", "LanmanServer", "Spooler" };
            services.AddRange( serviceArr );

#else
            // Get the list of additional services from the config file.
            Settings appSettings = Settings.Default;
            StringCollection services = appSettings.Services;
            
            // Add the EPMAuditing Service to the form
            serviceControl = new ServiceControl();
            serviceControl.Show();
            serviceControl.StartServiceControl("EPMAuditing Receiver Service", "Refresh Lsteners", (int)SimpleServiceCustomCommand.ExecuteRefreshListenerList);
            serviceControl.Location = new Point(x, y);
            this.Controls.Add(serviceControl);
            _controlList.Add(serviceControl);

            // Set form size based on the two EPM Sync base controls
            formWidth = serviceControl.Width * 2;
            formHeight = serviceControl.Height;

#endif
            // Loop through to add additionally spcified services
            int i = 0;
            foreach (string service in services)
            {
                serviceControl = new ServiceControl();
                // Determine X position
                if ((i % 2) == 0)
                {
                    x = 0;
                }
                else
                {
                    x = serviceControl.Size.Width;
                    formWidth = serviceControl.Size.Width * 2;
                }

                y = serviceControl.Height * ((i / 2) + 1);
                formHeight = serviceControl.Height * ((i / 2) + 2);

                // Setup control at the correct position
                serviceControl.Location = new Point(x, y);
                serviceControl.Show();
                serviceControl.StartServiceControl(service);
                this.Controls.Add(serviceControl);
                this.Size = new Size(formWidth + _widthOffset, formHeight + _heightOffset);
                _controlList.Add(serviceControl);
                i++;
            }

        }

        private void frmDisplay_FormClosing(object sender, FormClosingEventArgs e)
        {
            foreach (ServiceControl serviceControl in _controlList)
            {
                serviceControl.StopServiceControl();
            }
        }
    }
}